package com.gotop.auth.granter;

import com.gotop.common.exception.SecureException;
import com.gotop.common.utils.ApplicationStringUtils;
import lombok.AllArgsConstructor;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: cem
 * @description:
 * @author: superfish
 * @create: 2020-12-17 15:56
 **/
@AllArgsConstructor
public class TokenGranterBuilder {

    /**
     * TokenGranter缓存池
     */
    private static final Map<String, ITokenGranter> GRANTER_POOL = new ConcurrentHashMap<>();

    static {
        GRANTER_POOL.put(PasswordTokenGranter.GRANT_TYPE, ApplicationStringUtils.getBean(PasswordTokenGranter.class));
        GRANTER_POOL.put(CaptchaTokenGranter.GRANT_TYPE, ApplicationStringUtils.getBean(CaptchaTokenGranter.class));
        GRANTER_POOL.put(RefreshTokenGranter.GRANT_TYPE, ApplicationStringUtils.getBean(RefreshTokenGranter.class));
//        GRANTER_POOL.put(SocialTokenGranter.GRANT_TYPE, StringUtils.getBean(SocialTokenGranter.class));
    }

    /**
     * 获取TokenGranter
     *
     * @param grantType 授权类型
     * @return ITokenGranter
     */
    public static ITokenGranter getGranter(String grantType) {
        ITokenGranter tokenGranter = GRANTER_POOL.get(ApplicationStringUtils.toStr(grantType, PasswordTokenGranter.GRANT_TYPE));
        if (tokenGranter == null) {
            throw new SecureException("no grantType was found");
        } else {
            return tokenGranter;
        }
    }

}